package main

import (
	".."
	"fmt"
	"sync"
	"time"
)

type ProtoEcho struct {
	buff []byte
}

func (p *ProtoEcho) Msg() []byte {
	return p.buff
}

type CallBackEcho struct {
}

func (c CallBackEcho) OnReadMsg(conn *network.Connection) (network.Message, error) {
	fmt.Println("OnReadMsg")
	proto := &ProtoEcho{
		buff: make([]byte, 1024),
	}
	_, err := conn.Conn.Read(proto.buff)
	if err != nil {
		return nil, err
	}
	return proto, nil
}

func (c CallBackEcho) OnConnection(conn *network.Connection) {
	fmt.Println("OnConnection")
	var msg ProtoEcho
	msg.buff = []byte("hello world")
	conn.SendPacket(&msg)
}
func (c CallBackEcho) OnMessage(conn *network.Connection, msg network.Message) {
	fmt.Println("OnMessage")
	fmt.Println(msg.Msg())
}
func (c CallBackEcho) OnClose(conn *network.Connection) {
	fmt.Println("OnClose")
}

func main() {
	var wg sync.WaitGroup
	for i := 0; i < 100;i++ {
		wg.Add(1)
		go func() {
			tcp_client := network.NewTcpClient(CallBackEcho{})
			tcp_client.Start("127.0.0.1", 8989)
			time.Sleep(5 *time.Second)
			wg.Done()
		}()
	}
	wg.Wait()
}